home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / cpphtp2 / code.jar / code / ch03 / fig03_14.txt < prev    next >
Text File  |  1998-02-27  |  632b  |  24 lines

  1. 1   // Fig. 3.14: fig03_14.cpp
  2. 2   // Recursive factorial function
  3. 3   #include <iostream.h>
  4. 4   #include <iomanip.h>
  5. 5   
  6. 6   unsigned long factorial( unsigned long );
  7. 7   
  8. 8   int main()
  9. 9   {
  10. 10     for ( int i = 0; i <= 10; i++ )
  11. 11        cout << setw( 2 ) << i << "! = " << factorial( i ) << endl;
  12. 12  
  13. 13     return 0;
  14. 14  }
  15. 15  
  16. 16  // Recursive definition of function factorial
  17. 17  unsigned long factorial( unsigned long number )
  18. 18  {
  19. 19     if ( number <= 1 )  // base case
  20. 20        return 1;
  21. 21     else                // recursive case
  22. 22        return number * factorial( number - 1 ); 
  23. 23  } 
  24.